﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Acr.XamForms.UserDialogs;
using Ninject.Activation.Caching;
using SpotOffer.AppConstants;
using SpotOffer.CustomViews;
using SpotOffer.DataModels;
using SpotOffer.Resource;
using SpotOffer.Services;
using SpotOffer.Utils;
using SpotOffer.View;
using Xamarin.Forms;
using XLabs.Forms.Mvvm;

namespace SpotOffer.ViewModels
{
    public class ChooseCityViewModel : BaseViewModel
    {
        public override void OnViewAppearing()
        {
            base.OnViewAppearing();
            GetCityAsync();
        }

        public List<City> CityList
        {
            get { return _cityList; }
            set
            {
                if (value != null)
                {
                    _cityList = value;
                    NotifyPropertyChanged();
                }
            }

        }
        private List<City> _cityList;
        public City SelectedCity
        {
            get { return _selectedCity; }
            set
            {
                if (value != null & _selectedCity != value)
                {
                    //----------------------Styling---------------//
                    foreach (var city in _cityList)
                    {
                        city.Higlighted = false;
                    }

                    _selectedCity = value;
                    _selectedCity.Higlighted = true;
                    //----------------------Styling---------------
                    _selectedCity = value;
                    App.Cache.Set(CacheKeys.City, _selectedCity);
                   /* Navigation.PopAsync(true).ContinueWith((sender) =>
                    {
                        App.AppInstance.MasterPage.IsPresented = true;
                    });*/
                  //  MessagingCenter.Send(this, MessagingConstants.City, SelectedCity);
                }
            }
        }
        private City _selectedCity;

        public string CitySettingTitle
        {
            get
            {
                var cachedcity = App.Cache.Get<City>(CacheKeys.City);
                if (cachedcity == null)
                {
                    return AppResources.Chosen + " " + AppResources.Nothing;

                }
                else return /*AppResources.Chosen + " " +*/ cachedcity.name;
            }
        }

        public async void GetCityAsync()
        {
            Dialogs.ShowLoading(AppResources.Loading);
            try
            {
                {
                    var templist = await DependencyService.Get<ICityCategoryService>().GetCitiesCategories();
                    // Check if new deals is selected or category


                    var cachedCity = App.Cache.Get<City>(CacheKeys.City);
                    if (cachedCity != null)
                    {
                        templist.cities.First(city => city.id == cachedCity.id).Higlighted = true;
                    }

                    CityList = templist.cities;

                    await Task.Delay(600);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e + " " + e.Message);
                Dialogs.HideLoading();
                var ac = new AlertConfig { Message = e.Message };
                Dialogs.Alert(ac);
            }
            finally
            {
                Dialogs.HideLoading();
            }

        }
    }
}
